package cn.iocoder.yudao.module.famous.doctor.controller.app.famOcr;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.famous.doctor.controller.app.famOcr.util.UseUtil;
import cn.iocoder.yudao.module.infra.service.file.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import java.io.IOException;
import java.util.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @author zhangjiasong
 * @description: TODO
 * @date 2023/11/1 14:02
 */
@Tag(name = "用户 APP   ocr")
@RestController
@RequestMapping("/famous.doctor/fam-ocr/app")
@Validated
public class FamOcrController {
    @Resource
    public UseUtil util;
    @Resource
    private FileService fileService;

    @PostMapping("/useocr")
    @Operation(summary = "ocr")
    @Parameter(name = "useocr", description = "ocr图文识字", required = true)
    public CommonResult<TreeMap<String, Object>> useocr(MultipartFile[] file) throws Exception {
        try {
            List<String> base64Encodedlist = new ArrayList<>();

            // 将文件内容进行Base64编码
            for (MultipartFile multipartFile : file) {
                base64Encodedlist.add(Base64.getEncoder().encodeToString(multipartFile.getBytes()));
            }
            // 打印结果
            TreeMap<String, Object> strings = util.useOcr(base64Encodedlist);
            return success(strings);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return success(null);
    }

}
